'use strict';

const BaseController = require('./base');

class ParentController extends BaseController {
  constructor(ctx) {
    super(ctx, 'parent');
  }

  // 微信登录
  async wxLogin() {
    const { ctx } = this;
    const data = ctx.request.body;
    const result = await ctx.service.parent.wxLogin(data);
    ctx.success(result);
  }

  // 获取孩子列表
  async getChildren() {
    const { ctx } = this;
    const { parent_id } = ctx.query;
    const result = await ctx.service.parent.getChildren(parent_id);
    ctx.success(result);
  }

  // 查看孩子的课程记录
  async getChildCourses() {
    const { ctx } = this;
    const { student_id } = ctx.query;
    const result = await ctx.service.parent.getChildCourses(student_id);
    ctx.success(result);
  }

  // 重写 getPageQuery 方法，用于列表筛选
  getPageQuery(query) {
    const { name, phone, email, status } = query;
    const where = {};

    if (name) where.name = { [this.app.Sequelize.Op.like]: `%${name}%` };
    if (phone) where.phone = { [this.app.Sequelize.Op.like]: `%${phone}%` };
    if (email) where.email = { [this.app.Sequelize.Op.like]: `%${email}%` };
    if (status) where.status = status;

    return where;
  }

  // 重写 getListQuery 方法，用于全部列表筛选
  getListQuery(query) {
    return this.getPageQuery(query);
  }
}

module.exports = ParentController;
